#ifndef DATAMANAGER_H
#define DATAMANAGER_H

#include "BaseManager.h"
#include "Macros.h"

#include <QKeyEvent>
#include <QSet>
#include <QTranslator>

class QTranslator;

#define qData DataManager::instance()

// Always call this macro at the end of constructor
#define Q_TR_NOTIFY(T)                                                                             \
    reloadStrings();                                                                               \
    connect(qData, &DataManager::stringUpdated, this, &T::reloadStrings);

class DataManager : public BaseManager {
    Q_OBJECT
    Q_SINGLETON(DataManager)
public:
    explicit DataManager(QObject *parent = nullptr);
    ~DataManager();

public:
    bool load() override;
    bool save() override;

    void reloadStrings();

public:
    bool translate(const QString &filename);
    void eliminate();

    void loadLocale(int index);
    int localeCount() const;
    QStringList localeNames() const;

    static QString tempDir();
    static QString desktopDir();

    static QString openDir(const QString &title, const QString &flag);

private:
    QSet<QTranslator *> Translators;

    static QMap<QString, QString> lastOpenPaths;

    static QString getLastOpenPath(const QString &type);
    static void saveLastOpenDir(const QString &type, const QString &path);

public:
    QString MainTitle;
    QString WindowTitle;
    QString ErrorTitle;
    QString UntitledFileName;
    QString UntitledPrefix;
    QString DeletedPrefix;

    QString fileManagerName;

signals:
    void stringUpdated();
};

#endif // DATAMANAGER_H
